﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WeiboAutomation
{
    public class Logger
	{
        #region logger
        public void logger_file(string content)
        {
            DateTime currentTime = DateTime.Now;
            string fname = currentTime.ToString("yyyy") + currentTime.ToString("MM") + ".log";
            FileInfo finfo = new FileInfo(fname);
            if (finfo.Exists && finfo.Length > 1024000)
            {
                finfo.Delete();
            }
            using (FileStream fs = finfo.OpenWrite())
            {
                string current_record = currentTime.ToString("d") + " " + currentTime.ToString("T") + " " + content;
                StreamWriter w = new StreamWriter(fs);
                w.BaseStream.Seek(0, SeekOrigin.End);//设置写数据流的起始位置为文件流的末尾  
                w.Write(current_record + "\r\n");  //写入日志内容并换行
                w.Flush();
                w.Close();  //关闭写数据流  
            }
        }
        public void logger_screen(string content)
        {
            DateTime currentTime = DateTime.Now;
            string current_record = currentTime.ToString("d") + " " + currentTime.ToString("T") + " " + content;
            Console.WriteLine(current_record);
        }
        public void log(string content)
        {
            logger_screen(content);
            logger_file(content);
        }
        #endregion
	}
}
